﻿
var InsertCalculatorGifts = function (giftsTypeElement, giftsElement, totalDayElement, nlDayAdultElement, teDayChildElement,
    tnDayBabyElement, priceAdultElement, priceChildElement, priceBabyElement, quantityAdultElement,
    quantityChildElement, quantityBabyElement, vatElement, noteElement, totalElement, tourIdElement, userIdElement) {

    //ham tinh tong gia
    var fncTotalPriceGifts = function () {
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var priceBaby = $(priceBabyElement).val();
        var quantityAdult = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var quantityBaby = $(quantityBabyElement).val();
        var totalDay = $(totalDayElement).val();
        var nlDayAdult = $(nlDayAdultElement).val();
        var teDayChild = $(teDayChildElement).val();
        var totalprice = 0;
        var tnDayBaby=$(tnDayBabyElement).val();
        totalprice = ((priceAdult * quantityAdult * nlDayAdult) + (priceChild * quantityChild * teDayChild)
                + (priceBaby * quantityBaby * tnDayBaby)) * totalDay;
        $(totalElement).text(totalprice);
        $(vatElement).val(totalprice*0.1);
    };
    $(quantityAdultElement).keyup(fncTotalPriceGifts);
    $(quantityChildElement).keyup(fncTotalPriceGifts);
    $(quantityBabyElement).keyup(fncTotalPriceGifts);
    $(priceAdultElement).keyup(fncTotalPriceGifts);
    $(priceChildElement).keyup(fncTotalPriceGifts);
    $(priceBabyElement).keyup(fncTotalPriceGifts);
    $(nlDayAdultElement).keyup(fncTotalPriceGifts);
    $(teDayChildElement).keyup(fncTotalPriceGifts);
    $(tnDayBabyElement).keyup(fncTotalPriceGifts);
    $(totalDayElement).keyup(fncTotalPriceGifts);

   //Load danh sách Gifts
    var fncLoadGifts = function (giftsId,giftsName) {
        var giftsTypeId = $(giftsTypeElement).val();
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "GiftsByGiftsType", "GiftsTypeId": giftsTypeId },
            dataType: "json",
            success: function (data) {
                $(giftsElement).html('<option value="-1">--- Chọn loại quà ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(giftsElement)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                        
                    });
                    $(giftsElement).combobox('setValue', giftsName);
                    $(giftsElement).val(giftsId);
                }
             }
        });
    };
    $(giftsTypeElement).combobox({
        select: function (event, ui) {
            fncLoadGifts();
        }
    });
    
    var fncLoadPriceGifts = function () {
        var giftsId = $(giftsElement).val();
        $(priceChildElement).val('0');
        $(priceAdultElement).val('0');
        $(priceBabyElement).val('0');
        $('#btnInsertGifts').attr('price', '-1');
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "PriceGifts", "GiftsId": giftsId },
            dataType: "json",
            success: function (data) {
                if (data && data.isError == false) {
                    $(priceAdultElement).val(data.PriceAdult);
                    $(priceChildElement).val(data.PriceChild);
                    $(priceBabyElement).val(data.PriceBaby);
                    $('#btnInsertGifts').attr('price', data.PriceId);
                }
                fncTotalPriceGifts();
            }
        });
    };
    $(giftsElement).combobox({
        select: function (event, ui) {
            fncLoadPriceGifts();
        }
    });
    //ham them sua xoa
    var fncGifts = function () {
        var typeId = 13;
        var tourId = parseInt(tourIdElement);
        var giftsId = $(giftsElement).val();
        var giftsTypeId = $(giftsTypeElement).val();
        var quantityAdult = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var quantityBaby = $(quantityBabyElement).val();
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var priceBaby = $(priceBabyElement).val();
        var nlDayAdult = $(nlDayAdultElement).val();
        var totalDay = $(totalDayElement).val();
        var teDayChild = $(teDayChildElement).val();
        var tnDayBaby = $(tnDayBabyElement).val();
        var vat = $(vatElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        var note = $(noteElement).val();
        var userId = parseInt(userIdElement);
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: {
                "function": "InsertGifts", "TypeId": typeId, "TourId": tourId, "GiftsId": giftsId,
                "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "QuantityBaby": quantityBaby,
                "PriceAdult": priceAdult, "PriceChild": priceChild, "PriceBaby": priceBaby, "VAT": vat,
                "CalculatorId": calculatorId, "ClassName": className, "NlDay": nlDayAdult, "TeDay": teDayChild,
                "TnDay": tnDayBaby, "Note": note, "TotalDay": totalDay, 'UserId': userId
            },
            dataType: "json",
            success: function (data) {

                $(".ListGifts").remove();
                $(giftsTypeElement).val('-1');
                $(giftsElement).val('');
                $(priceAdultElement).val('');
                $(priceChildElement).val('');
                $(priceBabyElement).val('');
                $(quantityAdultElement).val('');
                $(quantityChildElement).val('');
                $(quantityBabyElement).val('');
                $(nlDayAdultElement).val('');
                $(teDayChildElement).val('');
                $(tnDayBabyElement).val('');
                $(vatElement).val('');
                $(totalElement).text('');
                $(noteElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-ticket-gifts')
                            .before($("<tr class='ListGifts'></tr>").attr("id", 'Gifts-' + item.CalculatorId).html('<td></td>' +
                                '<td >' + item.GiftsTypeName + '</td>' +
                                '<td >' + item.GiftsName + '</td>' +
                                '<td >' + item.TotalDay + '</td>' +
                                '<td >' + item.NlDay + '</td>' +
                                '<td >' + item.TeDay + '</td>' +
                                '<td >' + item.TnDay + '</td>' +
                                '<td >' + item.PriceAdult + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.PriceBaby + '</td>' +
                                '<td >' + item.QuantityAdult + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                '<td >' + item.TotalPrice + '</td>' +
                                '<td>' + item.VAT + '</td>' +
                                '<td>' + item.Note + '</td>' +
                                '<td>' +
                                '<input class="btnUpdateGifts" onclick="openPopup(\'ticket-gifts\')"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteGifts"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
                    $('#TotalGiftsPrices').html(data.TotalGiftsPrices);
                    $('#TotalGiftsPricesAdults').html(data.TotalGiftsPricesAdults);
                    $('#TotalGiftsPricesChild').html(data.TotalGiftsPricesChilds);
                    $('#TotalGiftsPricesBaby').html(data.TotalGiftsPricesBabys);
                    $('#TotalVatGifts').html(data.TotalVatGifts);
                    $('#btnInsertGifts').attr("calculatorid", "-1").val("Thêm");
                    TotalPrice();
                    fncTotalCalculator();
                    hidePopup('ticket-gifts');
                }

            }
        });
        
    };
    $('#btnInsertGifts').on("click", fncGifts);
    $('#btnDeleteGifts').on("click", fncGifts);
    $('table').on("click", 'tr >td >input.btnDeleteGifts', fncGifts);
    $('table').on("click", 'tr >td >input.btnUpdateGifts', function () {
        var calculatorId = $(this).attr("calculatorid");

        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "UpdateCalculatorGifts", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
                $('#Gifts-' + calculatorId).remove();
                $(giftsTypeElement).combobox('setValue', data.GiftsTypeName);
                $(giftsTypeElement).val(data.GiftsTypeId);
                fncLoadGifts(data.GiftsId, data.GiftsName);
                $(quantityAdultElement).val(data.QuantityAdult);
                $(quantityChildElement).val(data.QuantityChild);
                $(quantityBabyElement).val(data.QuantityBaby);
                $(priceAdultElement).val(data.PriceAdult);
                $(priceChildElement).val(data.PriceChild);
                $(priceBabyElement).val(data.PriceBaby);
                $(vatElement).val(data.Vat);
                $(totalElement).text(data.TotalPrice);
                $(noteElement).val(data.Note);
                $(nlDayAdultElement).val(data.NlDay);
                $(teDayChildElement).val(data.TeDay);
                $(tnDayBabyElement).val(data.TnDay);
                $(totalDayElement).val(data.TotalDay);
                

                $('#btnInsertGifts').attr("calculatorid", calculatorId).attr('price', data.PriceId).val("Lưu");
                $('.btnUpdateGifts').hide();
                $('#btnDeleteGifts').show();
            }
        });
    });
}